package com.klhappy.houyuan.utils.imageselector;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.view.banner.DisplayUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class PictureDialog implements OnPageChangeListener {

    private RelativeLayout relate;
    private Dialog mDialog;
    private View view;
    private ViewPager pager;
    private TextView text1;
    private TextView text2;
    private ImageView back;
    private List<View> lists = new ArrayList<View>();
    private int num = 1;

    public void setCurItem(int num){
        this.num = num;
    }

    public void setOnView(Context context, List<String> list) {
        // TODO Auto-generated method stub
        view = LayoutInflater.from(context).inflate(R.layout.dialog_activity, null);
        relate = (RelativeLayout) view.findViewById(R.id.relate);
        pager = (ViewPager) view.findViewById(R.id.pager);
        text1 = (TextView) view.findViewById(R.id.text1);
        text2 = (TextView) view.findViewById(R.id.text2);
        back = (ImageView) view.findViewById(R.id.back_img);

        lists.clear();
        for (int i = 0; i < list.size(); i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_photo_view, null);
            TouchImageView photo = (TouchImageView) view.findViewById(R.id.detail_image);
            int width = DisplayUtil.getWindowWidth(context);
            int height = DisplayUtil.getWindowHeight(context);
            Picasso.with(context)
                    .load(list.get(i))
                    .placeholder(R.drawable.default_img)
                    .error(R.drawable.default_img)
                    .centerInside()
                    .resize(width, height)
                    .into(photo);
            lists.add(view);
        }

        MyAdapter adapter = new MyAdapter(context, lists);
        pager.setAdapter(adapter);
        pager.setCurrentItem(num);

        relate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                mDialog.dismiss();
            }
        });
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mDialog.dismiss();
            }
        });
        pager.setOnPageChangeListener(this);
        text1.setText((num + 1) + "");
        text2.setText(list.size() + "");

        mDialog = new Dialog(context, R.style.Dialog_Fullscreen);
        mDialog.setContentView(view);
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int i) {
        // TODO Auto-generated method stub
        text1.setText((i + 1) + "");
    }

    class MyAdapter extends PagerAdapter {
        private Context mContext;
        private List<View> list;

        public MyAdapter(Context mContext, List<View> list) {
            super();
            this.mContext = mContext;
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(list.get(position));
            return list.get(position);
        }
    }
}
